package zhongbo.java.air.dictionary.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.excption.ArchiveException;
import zhongbo.java.air.dictionary.excption.ExceptionCode;
import zhongbo.java.air.dictionary.mapper.AirportMapper;
import zhongbo.java.air.dictionary.model.Airport;
import zhongbo.java.air.dictionary.service.IAirportService;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class AirportServiceImpl implements IAirportService {
    @Autowired
    private AirportMapper airportMapper;

    @Override
    public Airport add(Airport port) {
        validAirport(port);
        port.setCreateTime(new Date());
        port.setUpdateTime(new Date());
        int insert = airportMapper.insert(port);
        if (insert > 0) {
            return port;
        }
        throw new RuntimeException("新增机场失败");

    }

    private void validAirport(Airport port) {
        if (port == null) {
            throw new RuntimeException("机场信息不能为空");
        }
        String code = port.getIcaoCode();
        String code1 = port.getIataCode();
        if (code.length() > 4) {
            throw new RuntimeException("ICAO不能超过4个字段");
        }
        if (code1.length() > 3) {
            throw new RuntimeException("IATA代码不能超过3个字段");
        }
        List<Airport> airports = airportMapper.selectList(null);
        for (Airport airport : airports) {
            if (code.equals(airport.getIcaoCode()) && code1.equals(airport.getIataCode())) {
                throw new ArchiveException(ExceptionCode.UNIQUE_FIELD);
            }
        }
//        APStatus status = port.getStatus();
//        if (status !=APStatus.CLOSED && status !=APStatus.OPERATIONAL && status !=APStatus.RESTRICTED){
//            throw new RuntimeException("只能是枚举值：OPERATIONAL-运行中, CLOSED-已关闭, RESTRICTED-限制运行");
//        }
    }

    @Override
    public String delete(String id) {
        if (StrUtil.isBlank(id)) {
            throw new ArchiveException(ExceptionCode.NOT_BE_EMPTY);
        }
        int delete = airportMapper.deleteById(id);
        if (delete > 0) {
            return id;
        }
        throw new ArchiveException(ExceptionCode.CANNOT_BE_DELETE);

    }

    @Override
    public Airport edit(Airport port1) {
        validAirport(port1);
        String id = port1.getId();
        if (StrUtil.isBlank(id)) {
            throw new ArchiveException(ExceptionCode.NOT_BE_EMPTY);
        }
        Airport port = airportMapper.selectById(id);
        if (port == null) {
            throw new ArchiveException(ExceptionCode.CANNOT_BE_MODIFIED);
        }
        int update = airportMapper.updateById(port1);
        port1.setUpdateTime(new Date());
        if (update > 0) {
            return port1;
        }
        throw new RuntimeException("修改机场失败");
    }

    @Override
    public Airport findOne(String id) {
        if (StrUtil.isBlank(id)) {
            throw new ArchiveException(ExceptionCode.NOT_BE_EMPTY);
        }
        Airport port = airportMapper.selectById(id);
        if (port == null) {
            throw new ArchiveException(ExceptionCode.CANNOT_BE_QUERY);
        } else {
            return port;
        }
    }

    @Override
    public PageVo<Airport> findPage(String keyword,String fields, Integer pageNumber, Integer pageSize, String oderByField, Boolean oderByAsc) {
        QueryWrapper<Airport> queryWrapper = new QueryWrapper<>();
        //支持指定字段模糊搜索Keyword
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)) {
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field), keyword);
            }

        }
        //检测是否有排序
        if (StrUtil.isNotBlank(oderByField)) {
            //检测是正序还是倒序
            boolean isOderByAsc = Boolean.TRUE.equals(oderByAsc);
            if (isOderByAsc) {
                //正
                queryWrapper.orderByAsc(oderByField);
            } else {
                //倒序
                queryWrapper.orderByAsc(oderByField);
            }
        }
        Page<Airport> pageParam = new Page<>(pageNumber, pageSize);
        Page<Airport> airportPage = airportMapper.selectPage(pageParam, queryWrapper);

        return new PageVo<>(airportPage);

    }
}
